using System;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Org.BouncyCastle.Crypto.Parameters;
using static Zeze.Util.Cert;

namespace UnitTest.Zeze.Util;

[TestClass]
public class TestCert
{
    public static void Test1()
    {
        /*
        const string pkcs12File = "test.pkcs12";
        const string passwd = "123";
        const string alias = "test";
        var data = Encoding.UTF8.GetBytes("data");

        if (!File.Exists(pkcs12File))
        {
            using var fs1 = new FileStream(pkcs12File, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
            var keyPair = GenerateRsaKeyPair();
            SaveKeyStore(fs1, passwd, alias, keyPair.Public, keyPair.Private, "test", 365);
        }

        using var fs2 = new FileStream(pkcs12File, FileMode.Open, FileAccess.Read, FileShare.Read);
        var keyStore = LoadKeyStore(fs2, passwd);
        var publicKey = GetPublicKey(keyStore, alias);
        var privateKey = GetPrivateKey(keyStore, alias);

        if (File.Exists("signature"))
        {
            var signature = File.ReadAllBytes("signature");
            var verify = VerifySign(publicKey, data, signature);
            Assert.AreEqual(256, signature.Length);
            Assert.IsTrue(verify);
        }

        var signature2 = Sign(privateKey, data);
        var verify2 = VerifySign(publicKey, data, signature2);
        Assert.AreEqual(256, signature2.Length);
        Assert.IsTrue(verify2);

        using var aesKey = GenerateAesKey();
        var aesKeyData = aesKey.Key;
        var aesKeyEnc = EncryptRsa(publicKey, aesKeyData);
        var aesKeyDec = DecryptRsa(privateKey, aesKeyEnc);
        Assert.AreEqual(256, aesKeyEnc.Length);
        Assert.AreEqual(32, aesKeyDec.Length);
        Assert.IsTrue(aesKeyData.SequenceEqual(aesKeyDec));

        var iv = GenerateAesIv();
        var dataEnc = EncryptAes(aesKey, iv, data);
        var dataDec = DecryptAes(aesKey, iv, dataEnc);
        Assert.AreEqual(16, dataEnc.Length);
        Assert.AreEqual(4, dataDec.Length);
        Assert.IsTrue(data.SequenceEqual(dataDec));

        var aesKeyDecWithPadding = DecryptRsaNoPadding(privateKey, aesKeyEnc);
        Console.WriteLine("d1 = " + BitConverter.ToString(aesKeyDecWithPadding));
        Console.WriteLine("d0 = " + BitConverter.ToString(aesKeyData));

        File.WriteAllBytes("e.data", dataEnc);
        File.WriteAllBytes("iv", iv);
        File.WriteAllBytes("ekey", aesKeyEnc);

        aesKeyEnc = new byte[]
        {
            0x05, 0x4C, 0xFD, 0xDB, 0x64, 0x2F, 0xFB, 0x32, 0x14, 0x49, 0xF5, 0xC9, 0x8A, 0x70, 0xF2, 0x1E, 0x28, 0x88, 0x64, 0x27, 0x36, 0x83, 0xA6, 0xD0,
            0x38, 0x0D, 0xAF, 0x7E, 0x57, 0x7E, 0x5B, 0x92, 0x43, 0x90, 0x96, 0x26, 0x51, 0x44, 0x87, 0xB3, 0x91, 0x25, 0x74, 0xC8, 0x0E, 0xCD, 0x4F, 0x4C,
            0x96, 0xC3, 0xEB, 0xF9, 0xEA, 0xFB, 0x06, 0xC0, 0xF2, 0xD2, 0xE3, 0x45, 0x1F, 0x76, 0xBD, 0xF6, 0x4E, 0xAB, 0xBD, 0x66, 0x98, 0x42, 0x68, 0x6A,
            0x81, 0xD7, 0x30, 0x7B, 0x28, 0xA2, 0x59, 0x2B, 0x25, 0xAF, 0x84, 0xDC, 0x30, 0xC4, 0x69, 0x16, 0xBE, 0xC8, 0x13, 0xF0, 0x8C, 0x9B, 0x9E, 0x58,
            0xF6, 0x3B, 0xAB, 0x33, 0xB7, 0x89, 0xA2, 0xAC, 0x4E, 0x22, 0x01, 0x0F, 0xDA, 0xD9, 0xAF, 0x57, 0xC3, 0x5C, 0x42, 0x88, 0x43, 0x3C, 0x78, 0x30,
            0xC7, 0x46, 0x5B, 0x43, 0x7C, 0x52, 0x9E, 0x03, 0xD8, 0x23, 0x1D, 0xF1, 0x91, 0x75, 0x33, 0x8F, 0xAA, 0x71, 0x0E, 0xAB, 0x18, 0xD2, 0x86, 0xAA,
            0x90, 0x8F, 0x17, 0x57, 0x4A, 0xF4, 0x1E, 0xD9, 0xBF, 0xB6, 0xC3, 0xCC, 0x6E, 0x22, 0x7F, 0x6F, 0xDA, 0x28, 0x20, 0x31, 0x07, 0x12, 0x50, 0x11,
            0xB9, 0x29, 0xC4, 0xAB, 0xA7, 0xB5, 0x41, 0x5A, 0x7E, 0xA0, 0x97, 0x3B, 0x2B, 0x09, 0xA3, 0xCD, 0x8F, 0xB4, 0x9D, 0xE8, 0x11, 0xB5, 0xEA, 0x22,
            0x8F, 0xBB, 0x9B, 0x02, 0x00, 0x7C, 0x58, 0x61, 0xF0, 0xA5, 0x57, 0x7D, 0x9E, 0x64, 0x4C, 0xF4, 0x6D, 0x54, 0x69, 0xFD, 0x79, 0xA6, 0x2A, 0xCB,
            0x45, 0x93, 0xAC, 0xB2, 0xE6, 0x4D, 0x12, 0x2F, 0xAC, 0xA8, 0xE9, 0x61, 0xAC, 0x36, 0xDA, 0x43, 0xF9, 0xAE, 0x81, 0x0B, 0x7E, 0x83, 0x68, 0xEC,
            0x17, 0x8A, 0xE7, 0xF3, 0x29, 0xFC, 0xF9, 0x37, 0xFA, 0xCC, 0xC3, 0x4D, 0x43, 0x0C, 0x80, 0x1D
        };
        aesKeyDec = DecryptRsa(privateKey, aesKeyEnc);
        Assert.AreEqual(256, aesKeyEnc.Length);
        Assert.AreEqual(32, aesKeyDec.Length);
        Console.WriteLine(BitConverter.ToString(aesKeyDec));
        iv = new byte[] { 0x0E, 0x8A, 0xF9, 0x2E, 0xCF, 0xD1, 0x2A, 0x81, 0x3A, 0xE6, 0xBA, 0x6E, 0x49, 0x1C, 0xA4, 0x07 };
        dataEnc = new byte[] { 0x1E, 0x50, 0x16, 0x39, 0x40, 0xE4, 0xA6, 0x67, 0x5F, 0x23, 0xF7, 0x83, 0xBF, 0x22, 0xE8, 0x1E };
        using var aesKey2 = LoadAesKey(aesKeyDec);
        dataDec = DecryptAes(aesKey2, iv, dataEnc);
        Assert.IsTrue(data.SequenceEqual(dataDec));

        var publicKeyData = ((RsaKeyParameters)publicKey).Modulus.ToByteArray();
        Console.WriteLine("rsa modulus = [" + publicKeyData.Length + "] " + BitConverter.ToString(publicKeyData));
        */
    }
}
